<?php
namespace Home\Controller;

class IndexController extends \Think\Controller {
	
	public function index(){
		$this->display();
	}
    public function doLogin() {
        $name   = I('post.name', '', 'trim');
        $passwd = I('post.passwd', '', 'trim');
		$status=I('post.status');
        $msg    = array();

        if (empty($name)) {
            $msg[] = '请输入登陆名称';
        }

        if (empty($passwd)) {
            $msg[] = '请输入密码';
        }

        if (!empty($msg)) {
            alert(implode("\n", $msg));
        }
		
		
    
        $userModel = D('User');

        try {
            $userRecord = $userModel->getLogin($name, $passwd);

            /**
             * 只有站长和广告主才能返回地址，所以如果没有返回地址，可以认定肯定不是站长或广告主
             */
            if(!$loginToUrl = $userModel->getLoginToUrlByType($userRecord['type'])) {
                alert('输入的用户名或密码有误');
            }
            if (!$appName = $userModel->getAppNameByType($userRecord['type'])) {
                alert('会员类型有误，无法登陆');
            }

            $userModel->setLoginAppName($appName);
            $userModel->setLoginUser($userRecord, $appName);

            echo <<<EOD
			<script>
			top.window.location.href = '{$loginToUrl}';
			</script>
EOD;
            exit();
        } catch(\Exception $e) {
            alert($e->getMessage());
        }
    }

    /**
     * getLogin
     * 返回已经登陆成功的会员数据
     *
     * @return void
     */
    public function getLogin() {
        $mUser = D2('User');

        if((!$rUser = $mUser->getLoginUser(ADER_APP_NAME)) && (!$rUser = $mUser->getLoginUser(SITER_APP_NAME))) {
            showJson(array('s' => false));
        }

        if(!$loginToUrl = $mUser->getLoginToUrlByType($rUser['type'])) {
            showJson(array('s' => false));
        }

        $dao = array(
            'user_name'       => $rUser['user_name'],
            'last_login_time' => $rUser['last_login_time'],
            'last_login_ip'   => $rUser['last_login_ip'],
            'login_to_url'    => $loginToUrl,
        );

        showJson(array('s' => true, 'd' => $dao));
    }

    public function doPasswd() {
        $oldPasswd = I('post.old_passwd', '');
        $newPasswd = I('post.new_passwd', '');
        $msg       = array();

        if(empty($oldPasswd)) {
            $msg[] = '请输入旧密码';
        }

        if(empty($newPasswd)) {
            $msg[] = '请输入新密码';
        }

        if(empty($msg) && $oldPasswd == $newPasswd) {
            $msg[] = '新密码不同与老密码相同';
        }

        if(!empty($msg)) {
            showJson(array('s' => false, 'm' => implode("\n", $msg)));
        }

        $mUser = D2('User');
        $rUser = $mUser->getLoginUser();

        if(!$rUser = $mUser->getByUid($rUser['uid'])) {
            showJson(array('s' => false, 'm' => '没有找到会员信息'));
        }

        if($rUser['password'] != md5($oldPasswd)) {
            showJson(array('s' => false, 'm' => '旧密码有误'));
        }

        $r = $mUser->where(array('uid' => $rUser['uid']))->setField('password', md5($newPasswd))
             ? array('s' => true, 'm' => '密码修改成功')
             : array('s' => false, 'm' => '密码修改出错');

        showJson($r);
    }

    /**
     * checkExistsName
     * 检测登陆名称是否已经存在
     *
     * @return void
     */
    public function checkExistsName() {
        $name  = I('get.name', '');
        $mUser = D2('User');
        $expr  = array('user_name' => $name);

        $r = $mUser->where($expr)->find() ? array('s' => false, 'm' => '登陆名称已经存在') : array('s' => true, 'm' => '');

        showJson($r);
    }

    public function logout() {
        $moduleName = I('get.f', '');
        D('User')->logout($moduleName);

        setcookie('menu-index', 0); //清零被选中菜单编号

        header('location:index.php?');
    }
	
	public function checkName(){
		if(IS_AJAX){
			$m = D2('User');
			if($m->getByName($_POST['name'])){
				echo json_encode('用户名已存在');
			}else{
				echo '';
			}
		}
	}
	
	public function reg(){
		try{
			$m = D2('User');
			$m->reg($_POST);
			$msg = ['s' => true , 'm' => '注册成功' ];
		}catch(\Common\Exception\Exception $e){
			$msg = ['s' => false , 'm' => $e->getError()];
		}
		showJson($msg);
	}

}
